Trò chơi game bắn súng đơn giản trong UNITY Engine
- Enemy.cs
- EnemiesScr /
- Scripts /
- Assets /
- project /
2 using System.Collections;
3
4 //enemy types: 1,2,3 - simple enemies; 0 - boss rings, 4 - boss body
5 //each enemyType has its own behaviour
6
7 public class Enemy : MonoBehaviour, IInterractWithBullet
8 {
9 SoundManager sounds;
10 public int lifeMax;
11 public int life;
12 Pools pools;
13 public int enemyType;
14 Rigidbody rb;
15 Transform player;//player position for navigate on player
16 UIgame uiForScore;
17
18 //moving fields
19 float movingSpeed;
20 Vector3 moveDirection;
21
22 void Awake ()
23 {
24 pools = GameObject.FindObjectOfType<Pools>();
25 rb = GetComponent<Rigidbody>();
26 sounds = GameObject.FindObjectOfType<SoundManager>();
27 uiForScore = GameObject.FindObjectOfType<UIgame>();
28 }
29
30 void Start()
31 {
32 if (enemyType != 0 && enemyType != 4)//not a boss or not a boss ring
33 {
34 if (SystemScr.difficultyIsHard)
35 lifeMax = 2;
36 else
37 lifeMax = 1;
38 }
39
40 life = lifeMax;
41 }
42
43 void Update()
44 {
45 if (enemyType == 2 || enemyType == 3)
46 {
47 transform.Rotate(new Vector3(0, 70, 0) * Time.deltaTime);
48 }
49 }
50
51 // Update is called once per frame
52 void FixedUpdate ()
53 {
54 if (enemyType == 0 || enemyType == 4)//boss elements
55 return;
56
57 else if (enemyType == 3)
58 {
59 moveDirection = (player.position - transform.position).normalized;
60 }
61
62 rb.velocity = moveDirection * movingSpeed;
63
64 }
65
66 public void Activation(Vector3 revealPosition, Vector3 _moveDirection)
67 {
68 transform.position = revealPosition;
69
70 if (enemyType == 2)
71 {
72 moveDirection = _moveDirection;
73 movingSpeed = 2;
74 }
75 else if (enemyType == 3)
76 {
77 movingSpeed = 1.2f;
78 }
79
80 life = lifeMax;
81
82 ShootingStart();
83 }
84
85 public void Activation()
86 {
87 ShootingStart();
88 }
89
90
91 public void GetDamage(int damage = 1)
92 {
93 sounds.PlaySoundsEnemy();
94 life -= damage;
95 if (life < 1)
96 Die();
97 }
98 //DROP BONUS AND DIE
99 public void Die()
100 {
101 GameObject bonus = pools.GetPoolableObject("bonus");
102 if (bonus != null)
103 {
104 bonus.SetActive(true);
105 bonus.GetComponent<Bonus>().Activation(transform.position);
106 }
107 if (enemyType == 4)
108 uiForScore.Win();
109 else
110 uiForScore.IncreaseScore(20);
111
112 transform.gameObject.SetActive(false);
113 }
114
115 void ShootingStart()
116 {
117 switch (enemyType)
118 {
119 case 1: StartCoroutine("ShootingRoutine_01"); break;
120 case 2: StartCoroutine("ShootingRoutine_02"); break;
121 case 3: StartCoroutine("ShootingRoutine_03"); break;
122 case 4: StartCoroutine("ShootingRoutine_04"); break;
123 }
124 }
125
126 //SHOOTING ROUTINE FOR ENEMY 1
127 IEnumerator ShootingRoutine_01()
128 {
129 for (int i = 0; i < 2; i++)
130 {
131 yield return new WaitForSeconds(1f);
132 Enemy_01_ShootCircle();
133 }
134 }
135
136 void Enemy_01_ShootCircle()
137 {
138 int bulletSpeed = 2;
139
140 GameObject bullet = pools.GetPoolableObject("e_bullet");
141
142 Vector3 direction = new Vector3(0,0,-1);
143
144 //
145 for (int i = 0; i < 32; i++)
146 {
147 bullet = pools.GetPoolableObject("e_bullet");
148 if (bullet == null)
149 {
150 continue;
151 }
152
153 bullet.SetActive(true);
154 bullet.GetComponent<EnemyBullet>().ShootMe(transform.position, direction.normalized, bulletSpeed);
155
156 //creat circle of bullets
157 if (direction.x < 1 && direction.z == -1)
158 direction.x += 0.25f;
159 else if (direction.z < 1 && direction.x == 1)
160 direction.z += 0.25f;
161 else if (direction.x > -1 && direction.z == 1)
162 direction.x -= 0.25f;
163 else if (direction.x == -1 && direction.z > -1)
164 direction.z -= 0.25f;
165 }
166 }
167
168 //SHOOTING ROUTINE FOR ENEMY 2
169 IEnumerator ShootingRoutine_02()
170 {
171 while (true)
172 {
173 int bulletSpeed = 7;
174
175 yield return new WaitForSeconds(2f);
176
177 GameObject bullet = pools.GetPoolableObject("e_bullet");
178
179 if (bullet != null)
180 {
181 bullet.SetActive(true);
182 bullet.GetComponent<EnemyBullet>().ShootMe(transform.position, new Vector3(0, 0, -1).normalized, bulletSpeed);
183 }
184 }
185 }
186
187 //SHOOTING ROUTINE FOR ENEMY 3
188 IEnumerator ShootingRoutine_03()
189 {
190 GameObject tempPlayer = GameObject.FindGameObjectWithTag("Player");
191
192 //if player didnt destroyed
193 if (tempPlayer != null)
194 player = tempPlayer.GetComponent<Transform>();
195 else
196 player = transform;
197
198 GameObject bullet = null;
199 int bulletSpeed = 9;
200
201 while (true)
202 {
203 yield return new WaitForSeconds(1.5f);
204
205 bullet = pools.GetPoolableObject("e_bullet");
206
207 if (bullet != null)
208 {
209 bullet.SetActive(true);
210 bullet.GetComponent<EnemyBullet>().ShootMe(transform.position, (player.position - transform.position).normalized, bulletSpeed);
211 }
212 }
213 }
214
215 //BOSS SHOOTING
216 IEnumerator ShootingRoutine_04()
217 {
218 yield return new WaitForSeconds(1f);
219 int bulletSpeed = 4;
220
221 GameObject bullet = pools.GetPoolableObject("e_bullet");
222
223 Vector3 direction = new Vector3(0,0,0);
224 int rCount = 1;
225
226 while (true)
227 {
228 if (rCount == 1)
229 {
230 StartCoroutine("ShootingRoutine_01");
231 rCount = 0;
232 }
233 else
234 rCount++;
235
236 yield return new WaitForSeconds(0.3f);
237 for (int i = 0; i < 64; i++)
238 {
239 bullet = pools.GetPoolableObject("e_bullet");
240
241 if (bullet == null)
242 {
243 continue;
244 }
245
246 yield return new WaitForSeconds(0.1f);
247
248 direction = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f));
249
250 bullet.SetActive(true);
251 bullet.GetComponent<EnemyBullet>().ShootMe(transform.position, direction.normalized, bulletSpeed);
252
253 }
254 }
255 }
256
257 void OnTriggerExit(Collider col)
258 {
259 if (col.tag == "SpawnBorders" && enemyType != 4)
260 {
261 transform.gameObject.SetActive(false);
262 }
263 }
264 }
enemy types: 1,2,3 - simple enemies; 0 - boss rings, 4 - boss body
each enemyType has its own behaviour
Transform player;player position for navigate on player
moving fields
if (enemyType != 0 && enemyType != 4)not a boss or not a boss ring
Update is called once per frame
if (enemyType == 0 || enemyType == 4)boss elements
DROP BONUS AND DIE
SHOOTING ROUTINE FOR ENEMY 1
creat circle of bullets
SHOOTING ROUTINE FOR ENEMY 2
SHOOTING ROUTINE FOR ENEMY 3
if player didnt destroyed
BOSS SHOOTING